package com.baizhi.llw.service;

import com.baizhi.llw.dao.UserMapper;
import com.baizhi.llw.entity.User;
import com.baizhi.llw.entity.UserExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryAllPage(int page, int pageSize) {
        Map<String,Object> map = new HashMap<>();
        List<User> users = userMapper.selectByRowBounds(new User(),new RowBounds((page-1)*pageSize,pageSize));
        int totalCount = userMapper.selectCount(null);
        map.put("total",totalCount);
        map.put("page",page);
        map.put("rows",users);
        return map;
    }

    @Override
    public Map<String , Object> update(int id,String status) {
        Map<String,Object> map = new HashMap<String,Object>();
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i==1){
            map.put("message","操作成功");
            map.put("status",200);
        }else {
            map.put("message","操作失败");
            map.put("status",400);
        }
        return map;
    }
}
